#!/usr/bin/env python
# -*- coding: utf-8 -*- 

#Kiwi Bot
#Copyright (C) 2009 Mathieu "MathX" Xhonneux
#Modified by @nge1nCorsic4 in 2011

#This program is free software; you can redistribute it and/or modify
#it under the terms of the GNU General Public License as published by
#the Free Software Foundation; either version 2 of the License, or
#(at your option) any later version.

#This program is distributed in the hope that it will be useful,
#but WITHOUT ANY WARRANTY; without even the implied warranty of
#MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
#GNU General Public License for more details.

#You should have received a copy of the GNU General Public License along
#with this program; if not, write to the Free Software Foundation, Inc.,
#51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

import kiwi.plugin, kiwi.events, kiwi.config, kiwi.update, kiwi.lang
import threading

class admin(kiwi.plugin.Plugin):
	def onStart(self):
		self.commandsRegistered = {}
		self.bdAdmin = kiwi.bd.bd_admin()
		self.lang = kiwi.lang.Lang(self, 'admin')
		self.registerEvent(kiwi.events.EVT_CLIENT_SAY,self.onSay)
		self.initCommands = {
		'slap' : '000001',
		'nuke' : '000002',
		'kick' : '000003',
		'mute' : '000004',
		'admins' : '000005',
		'list' : '000006',
		'say' : '000007',
		'name' : '000008',
		'big' : '000009',
		'status' : '000010',
		'setlevel' : '000011',
		'veto' : '000012',
		'restart' : '000013',
		'reload' : '000014',
		'version' : '000015',
		'update' : '000016',
		'die' : '000019',
		'exec' : '000020',
		'help' : '000021',
		'red' : '000022',
		'blue' : '000023',
		'spec' : '000024',
		'hi' : '000025',
		'private' : '000026'
		}
		ServNumber = kiwi.config.configuration().kiwi.kiwidir
		self.conf = kiwi.config.configuration('@kiwi/conf/'+ServNumber+'/admin.cfg')
		for name, help in self.initCommands.iteritems():
			if hasattr(self.conf.levels,name):
				level = getattr(self.conf.levels,name)
				if level.isdigit():
					level = int(level)
					if level > -1 and level <= 10:
						if hasattr(self, '_'+name):
							self.commandsRegistered[name] = command(name, level, getattr(self,'_'+name), help)
					elif level == -1:
						pass
					else:
						self.main.log.warn(self.lang.lang('','00027')%(name))
				else:
					self.main.log.warn(self.lang.lang('','00027')%(name))
		self.godUsed = self.checkGod()
		self.passwd = False
		self.reload('reload',5)
	def onStop(self):
		pass
	def onSay(self, event):
		if len(event.message) > 0:
			if event.message[0] == '!':
				message = event.message.split()
				if len(message) >= 1:
					if message[0][1:] in self.commandsRegistered or (message[0][1:3] == "be" and message[0][1:3] in self.commandsRegistered) :
						event.command = message[0][1:]
						if len(message) > 1:
							event.arguments = message[1:]
						else:
							event.arguments = []
						client = self.clients.getClientById(event.id)
						if client:
							if event.command[0:2] == "be":
								cmd = self.commandsRegistered[event.command[0:2]]
							else:
								cmd = self.commandsRegistered[event.command]
							if client[0].rights >= cmd.level and cmd.level != -1:
								cmd.attr(event)
							else:
								self.output.tell(event.id, self.lang.lang(event,'00033')%(event.command))
					# elif message[0][0:3] == '!be':
						# pass
					elif message[0][1:] == 'iamgod':
						if self.godUsed:
							self._iamgod(event)
						else:
							self.output.tell(event.id,self.lang.lang(event,'00102'))
					else:
						self.output.tell(event.id,self.lang.lang('','00102'))
			elif self.passwd :
				if event.id == self.id:
					message = event.message.split()
					if len(message) == 1:
						self.password = message[0]
					else:
						self.output.tell(self.id,"^1%s"%self.lang.lang(event,"00046"))
	def registerCommand(self, name, level, functionAttr, help = None):
		self.commandsRegistered[name] = command(name, level, functionAttr, help)
	def deleteCommand(self,name):
		if name in self.commandsRegistered:
			del self.commandsRegistered[name]
			return True
		return False
	def checkGod(self):
		if len(self.clients.rights) == 0:
			if self.conf.settings.god == 'yes':
				return True
		return False
	def ParsePassword(self,event):
		if self.passwd:
			if self.password:
				self.output.send('mute %s'%event.id)
				self.output.tell(event.id,"^5%s ^1%s"%(self.lang.lang(event,"00042"),self.password))
				self.output.send('g_password %s'%self.password)
				self.passwd = False
				self.password = None
			else:
				self.output.tell(self.id,"^7%s"%self.lang.lang(event,"00044"))
			threading.Timer(5, self.ParsePassword,[event]).start()
	def reload(self,event,nb):
		self.output.bigtext('^4%s ^3%ss'%(self.lang.lang('','00074'),nb))
		if nb > 1 :
			nb -= 1
			threading.Timer(1, self.reload,[event,nb]).start()
		else:
			self.output.send('%s'%event)
	def _iamgod(self, event):
		client = self.clients.getClientById(event.id)[0]
		self.clients.setRights(client.guid,client.name,10)
		self.output.tell(event.id,self.lang.lang(event,'00034'))
		self.godUsed = False
	def _update(self, event):
		self.current = kiwi.update.Update(self)
		self.output.tell(event.id,self.lang.lang(event,'00051'))
		response = self.current.update()
		if response:
			maj = '^4%s %s.'%(self.lang.lang(event,'00052'),response[:-2])
		else:
			maj = self.lang.lang(event,'00053')
		self.output.tell(event.id,maj)
	def _version(self, event):
		path = kiwi.getPath('@version')
		f = file(path,'r')
		version = f.read()
		f.close()
		self.output.tell(event.id,version)
	def _slap(self, event):
		if len(event.arguments) > 0:
			clients = self.clients.getClientByInput(event.arguments[0])
			if len(clients) == 0:
				self.output.tell(event.id,'^4%s'%self.lang.lang('','00103'))
			elif len(clients) == 1:
				if len(event.arguments) > 1 :
					slapped = event.arguments[1]
					if slapped.isdigit():
						slapped = int(slapped)
						nb = 0
						if slapped <= 20 :
							while nb != slapped :
								self.output.send('slap %s'%(clients[0].id))
								nb = nb+1
						else :
							self.output.send('slap %s'%(event.id))
							self.output.tell(event.id,'^1%s'%self.lang.lang(event, '00056'))
					else:
						self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00057'))
				else:
					self.output.send('slap %s'%(clients[0].id))
			else:
				self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00104'))
		else:
			self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00101'))
	def _kick(self, event):
		if len(event.arguments) > 0:
			if len(event.arguments) > 1:
				clients = self.clients.getClientByInput(event.arguments[0])
				clientName = self.clients.getClientById(event.id)[0].name
				client = clients[0]
				if len(clients) == 0:
					self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00103'))
				elif len(clients) == 1:
					arguments = ' '.join(event.arguments[1:])
					self.output.send('kick %s'%(clients[0].id))
					self.main.log.kick(self.lang.lang('', '00058')%(clientName, client.name, arguments))
				else:
					self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00104'))
			else:
				self.output.tell(event.id,'^1%s'%self.lang.lang(event, '00059'))
		else:
			self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00101'))
	def _mute(self, event):
		if len(event.arguments) > 0:
			clients = self.clients.getClientByInput(event.arguments[0])
			if len(clients) == 0:
				self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00103'))
			elif len(clients) == 1:
				self.output.send('mute %s'%(clients[0].id))
			else:
				self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00104'))
		else:
			self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00101'))
	def _blue(self, event):
		if len(event.arguments) > 0:
			clients = self.clients.getClientByInput(event.arguments[0])
			if len(clients) == 0:
				self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00103'))
			elif len(clients) == 1:
				client = clients[0]
				friend = getattr(self.conf.levels,'friendlevel')
				if self.clients.getClientById(event.id)[0].rights >= int(friend) :
					self.output.send('forceteam %s blue'%(client.id))
				else:
					if client.id == event.id :
						self.output.send('forceteam %s blue'%(client.id))
					else:
						self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00032'))
			else:
				self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00104'))
		else:
			self.output.send('forceteam %s blue'%(event.id))
			# self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00101'))
	def _red(self, event):
		if len(event.arguments) > 0:
			clients = self.clients.getClientByInput(event.arguments[0])
			if len(clients) == 0:
				self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00103'))
			elif len(clients) == 1:
				client = clients[0]
				friend = getattr(self.conf.levels,'friendlevel')
				if self.clients.getClientById(event.id)[0].rights >= int(friend) :
					self.output.send('forceteam %s red'%(client.id))
				else:
					if client.id == event.id :
						self.output.send('forceteam %s red'%(client.id))
					else:
						self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00032'))
			else:
				self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00104'))
		else:
			self.output.send('forceteam %s red'%(event.id))
			# self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00101'))
	def _spec(self, event):
		if len(event.arguments) > 0:
			clients = self.clients.getClientByInput(event.arguments[0])
			if len(clients) == 0:
				self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00103'))
			elif len(clients) == 1:
				client = clients[0]
				friend = getattr(self.conf.levels,'friendlevel')
				if self.clients.getClientById(event.id)[0].rights >= int(friend) :
					self.output.send('forceteam %s spectator'%(client.id))
				else:
					if client.id == event.id :
						self.output.send('forceteam %s spectator'%(client.id))
					else:
						self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00032'))
			else:
				self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00104'))
		else:
			self.output.send('forceteam %s spectator'%(event.id))
			# self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00101'))
	def _say(self, event):
		if len(event.arguments) > 0:
			text = ' '.join(event.arguments)
			clientName = self.clients.getClientById(event.id)[0].name
			text = '^1(%s)^3 %s'%(clientName, text)
			self.output.say(text)
		else:
			self.output.tell(event.id, self.lang.lang(event,'00101'))
	def _big(self, event):
		if len(event.arguments) > 0:
			text = ' '.join(event.arguments)
			clientName = self.clients.getClientById(event.id)[0].name
			self.output.bigtext(text)
		else:
			self.output.tell(event.id, self.lang.lang(event,'00101'))
	def _help(self, event):
		if len(event.arguments) == 0:
			message = ('^4%s '%self.lang.lang(event, '00061'))
			for cmd in self.commandsRegistered.itervalues():
				if cmd.level <= self.clients.getClientById(event.id)[0].rights:
					message += '^3%s ^1|'%(cmd.name)
			message = message[:-3]+'^1.'
		else:
			cmd = event.arguments[0]
			if cmd in self.commandsRegistered:
				cmd = self.commandsRegistered[cmd]
				if cmd.level <= self.clients.getClientById(event.id)[0].rights:
					if cmd.help:
						if cmd.help.isdigit():
							help = self.lang.lang(event, cmd.help)
						else:
							help = cmd.help
						message = '^4%s ^1%s : ^3%s'%(self.lang.lang(event, '00062'), cmd.name, help)
					else:
						message = '^4%s ^3%s'%(self.lang.lang(event, '00063'), cmd.name)
				else:
					message = '^4%s ^3%s'%(self.lang.lang(event, '00063'), cmd.name)
			else:
				message = self.lang.lang(event,'00102')
		self.output.tell(event.id,message)
	def _setlevel(self, event):
		if len(event.arguments) > 1:
			if event.arguments[1].isdigit():
				level = int(event.arguments[1])
				if level > -1 and level < 15:
					client = self.clients.getClientByInput(event.arguments[0])
					if len(client) == 0:
						self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00103'))
					elif len(client) == 1:
						client = client[0]
						if self.clients.setRights(client.guid, client.name, level):
							# self.bdAdmin.setlevel(client.guid,client.name,level)
							self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00065'))
						else:
							self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00066'))
					else:
						self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00104'))
				else:
					self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00067'))
			else:
				self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00067'))
		else:
			self.output.tell(event.id,self.lang.lang(event,'00101'))
	def _admins(self, event):
		clients = self.clients.getClientsList()
		text = '^4%s'%self.lang.lang(event, '00068')
		for c in clients:
			if c.rights > 0:
				text += '^3%s ^1[%s]^3, '%(c.name,c.rights)
		text = text[:-2]+'.'
		self.output.tell(event.id,text)
	def _list(self, event):
		clients = self.clients.getClientsList()
		text = '^4%s'%self.lang.lang(event, '00069')
		for c in clients:
			text += '^3%s ^1[%s]^3, '%(c.name,c.id)
		text = text[:-2]+'.'
		self.output.tell(event.id,text)
	def _veto(self, event):
		self.output.send('veto')
	def _restart(self, event):
		nb = 5
		if len(event.arguments) > 0 :
			if event.arguments[0].isdigit():
				if int(event.arguments[0]) > 0:
					nb = int(event.arguments[0])
			else:
				self.output.tell(event.id,self.lang.lang('','00106'))
				return
		self.reload('restart',nb)
	def _reload(self, event):
		nb = 5
		if len(event.arguments) > 0 :
			if event.arguments[0].isdigit():
				if int(event.arguments[0]) > 0:
					nb = int(event.arguments[0])
			else:
				self.output.tell(event.id,self.lang.lang('','00106'))
				return
		self.reload('reload',nb)
	def _private(self, event):
		if self.passwd == False:
			if len(event.arguments) > 0:
				if event.arguments[0] == "off" :
					self.output.send('g_password ""')
					self.output.tell(event.id,self.lang.lang(event,'00041'))
				else:
					self.output.tell(event.id,self.lang.lang(event,'00043'))
			else:
				self.id = event.id
				self.passwd = True
				self.password = None
				self.output.send('mute %s'%event.id)
				self.ParsePassword(event)
		else:
			self.output.tell(event.id,self.lang.lang(event,'00047'))
	def _status(self, event):
		if len(event.arguments) > 0:
			clients = self.clients.getClientByInput(event.arguments[0])
			if len(clients) == 0:
				self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00103'))
			elif len(clients) == 1:
				client = clients[0]
				dns = client.dns
				if dns:
					dns = self.lang.lang(event, '00070')%dns
				else:
					dns = self.lang.lang(event, '00071')
				text = self.lang.lang(event, '00072') %(client.name,client.id,client.rights,client.ip,dns)
				self.output.tell(event.id, text)
			else:
				self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00104'))
		else:
			self.output.tell(event.id,self.lang.lang(event,'00101'))
	def _nuke(self, event):
		if len(event.arguments) > 0:
			clients = self.clients.getClientByInput(event.arguments[0])
			if len(clients) == 0:
				self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00103'))
			elif len(clients) == 1:
				self.output.send('nuke %s'%(clients[0].id))
			else:
				self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00104'))
		else:
			self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00101'))
	def _hi(self, event):
		client1 = self.clients.getClientById(event.id)[0]
		if len(event.arguments) > 0:
			client2 = self.clients.getClientByInput(event.arguments[0])
			if len(client2) == 0:
				self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00103'))
			elif len(client2) == 1:
				text = '^7%s ^4%s ^7%s.'%(client1.name,self.lang.lang(event, '00049'),client2[0].name)
				self.output.bigtext(text)
			else:
				self.output.tell(event.id,'^4%s'%self.lang.lang(event, '00104'))
		else:
			text = '^7%s ^4%s'%(client1.name,self.lang.lang(event, '00050'))
			self.output.bigtext(text)
	def _name(self, event):
		if len(event.arguments) > 0:
			clients = self.clients.getClientByInput(event.arguments[0])
			if len(clients) == 0:
				self.output.tell(event.id,self.lang.lang(event,"00103"))
			elif len(clients) == 1:
				names = self.bdAdmin.getAlias(clients[0].guid)
				self.output.tell(event.id, "^4%s ^7%s"%(self.lang.lang(event,"00001"),"^1, ^7".join(names)))
			else:
				self.output.tell(event.id,self.lang.lang(event,"00104"))
		else:
			self.output.tell(event.id,self.lang.lang(event,"00101"))
	def _exec(self, event):
		if len(event.arguments) > 0:
			self.output.send('exec %s'%(event.arguments[0]))
		else:
			self.output.tell(event.id,self.lang.lang(event,'00101'))
	def _die(self, event):
		self.output.tell(event.id, self.lang.lang(event,'00065'))
		self.main.main.onStop()

class command:
	def __init__(self, name, level, attr, help):
		self.name = name
		self.level = level
		self.attr = attr
		self.help = help
